// pages/common/feedback/index.js
import {isInputEmpty} from "../../../utils/Util";
import {uploader, feedback} from "../../../service/Common";
import Notify from "@vant/weapp/notify/notify";
import Dialog from "@vant/weapp/dialog/dialog";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShowType: true, //默认显示用户类型
        autoSize: {minHeight: 80}, //文本框尺寸设置
        suggest: "", //建议信息
        fileList: [], //上传信息
        contact: "", //联系方式
        checked: true, //复选框
        isLoading: false //提交按钮状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /*
    * 文本输入监听
    * */
    onInput: function (e) {
        let name = e.currentTarget.dataset.name;
        this.setData({[name]: e.detail})
    },

    /*
    * 类型选择接受函数
    * */
    onTypeSelect: function (e) {
        this.data._type = e.detail
        this.setData({isShowType: false})
    },

    //读取文件后
    onUploaderAfterRead: async function (e) {
        let index = e.detail.index;
        let tmpUrl = e.detail.file.url;
        try {
            this.setData({
                ["fileList[" + index + "]"]: {
                    url: tmpUrl,
                    status: "uploading",
                    message: "上传中"
                }
            })
            //上传服务器接口
            let res = await uploader(tmpUrl);

            //上传成功后
            this.setData({
                ["fileList[" + index + "].status"]: "",
                ["fileList[" + index + "].message"]: "",
                ["fileList[" + index + "].id"]: res
            })
        } catch (e) {
            this.setData({
                ["fileList[" + index + "].status"]: "failed",
                ["fileList[" + index + "].message"]: "上传失败",
            })
            console.error(e)
        }
    },
    //文件超出大小
    onUploaderOversize: function () {
        Notify({type: "warning", message: "上传文件超出大小限制"});
    },
    //删除上传文件
    onUploaderDelete: async function (e) {
        try {
            let index = e.detail.index;
            let fileList = this.data.fileList;
            this.setData({
                ["fileList[" + index + "].status"]: "uploading",
                ["fileList[" + index + "].message"]: "删除中"
            })
            //接下来 调用服务器删除接口

            //删除成功后
            fileList.splice(index, 1);
            this.setData({fileList})
        } catch (e) {

        }
    },


    /*
    * 复选框状态监听
    * */
    onCheckboxChange: function () {
        this.setData({checked: !this.data.checked})
    },

    /*
    * 提交按钮监听
    * */
    onSubmitButton: async function () {
        try {
            let type = this.data._type;
            let typeName = type.typeName + (type.errorName ? ("--" + type.errorName) : "");
            let suggest = this.data.suggest;
            let images = this.data.fileList.map(function (item) {
                return item.id
            }).join(",");
            let contact = this.data.contact;
            let checked = this.data.checked;
            let isLoading = this.data.isLoading;
            if (!isInputEmpty(suggest)) {
                Notify("请提供简要描述");
                return false
            } else if (isLoading) { //防重复
                return false
            } else {
                this.setData({isLoading: true});
                let postData = {
                    typeName,
                    suggest
                }
                images ? postData.images = images : null;
                contact ? postData.contact = contact : null;
                checked ? postData.checked = checked : null;
                await feedback(postData);
                this.setData({isLoading: false});
                await Dialog.alert({
                    message: "感谢你的支持，我们尽快修复问题！"
                })
                wx.navigateBack()
            }
        } catch (e) {
            this.setData({isLoading: false});
            console.error(e)
        }
    }


})